package com.cl.unipay4j.wx.splitbill;

import com.alibaba.fastjson.JSON;
import com.cl.unipay4j.core.IPayConfig;
import com.cl.unipay4j.core.Result;
import com.cl.unipay4j.core.UnipayConst;
import com.cl.unipay4j.core.exception.InvalidParamException;
import com.cl.unipay4j.core.splitbill.*;
import com.cl.unipay4j.core.util.MoneyUtil;
import com.cl.unipay4j.wx.AbstractWxPay;
import com.cl.unipay4j.wx.WxBizEnum;
import com.cl.unipay4j.wx.sdk.WXPayConstants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WxPaySplitBill
 * @Description
 * @Author Small
 * @Date 2020/8/4 18:14
 * @Version 1.0
 **/
public class WxPaySplitBillQuery extends AbstractWxPay implements ISplitBillQuery {
    public WxPaySplitBillQuery() {
    }

    public WxPaySplitBillQuery(IPayConfig payConfig) {
        super(payConfig);
    }


    private Map<String, String> wrapParam(ProfitQueryModel model) {
        Map<String, String> map = new HashMap<>();
        map.put("transaction_id",model.getTradeNo());
        map.put("out_order_no",model.getOutTradeNo());
        return map;
    }

    /**
     * 分账
     *
     * @param model
     * @return
     */
    @Override
    public Result split(ProfitQueryModel model) {
        try {
            Map<String, String> r = initWxPay().sendRequest(wrapParam(model),WXPayConstants.MULTIPROFITSHARING_URL_SUFFIX, WxBizEnum.SPLITBILL, true);
            String returnCode = r.get(WXPayConstants.RETURN_CODE);
            if (UnipayConst.SUCCESS.equals(returnCode)) {
                if (UnipayConst.SUCCESS.equals(r.get(WXPayConstants.RESULT_CODE))) {
                    return Result.tradeSuccess(r);
                } else {
                    String errCode = r.get(WXPayConstants.ERR_CODE);
                    String errCodeDes = r.get(WXPayConstants.ERR_CODE_DES);
                    return Result.returnFail(errCode, errCodeDes);
                }
            } else {
                String returnMsg = r.get(WXPayConstants.RETURN_MSG);
                return Result.returnFail(returnCode, returnMsg);
            }

        } catch (Exception e) {

        }
        return Result.exception();
    }
}
